var Koa = require('koa');

var router = require('koa-router')();

const mount = require('koa-mount');

const graphqlHTTP = require('koa-graphql');

var GraphQLDefaultSchema = require('./schema/default.js')


const DB = require('./model/db.js');

var app = new Koa();

//配置中间件
app.use(mount('/graphql', graphqlHTTP({
    schema: GraphQLDefaultSchema,
    graphiql: true
})));


router.get('/', async (ctx) => {
    ctx.body = "首页";
})
router.get('/articleSave', async (ctx) => {
    const crt_time = Date.now();
    const rander = Math.round(Math.random() * 100 + crt_time);
    const result = await DB.insert('articlecate', {
        title: '标题' + rander,
        description: '描述' + rander,
        keywords: '关键字' + rander,
        add_time: crt_time,
        status: rander,
    });
    ctx.body = result;
    ctx.status = 200;
})
router.get('/getNavList', async (ctx) => {

    var navList = await DB.find('nav', {});
    ctx.body = navList;
})

app.use(router.routes());   /*启动路由*/
app.use(router.allowedMethods());
app.listen(4000);



